/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Portions created by the Initial Developer are
 * Copyright (C) 2012 the Initial Developer.
 * All Rights Reserved.
 *
 * The Original Code is "http://semi-frame.googlecode.com/svn/trenk/".
 *
 * The Initial Developer of the Original Code is the author below.
 */
/**
 * @file .h
 * @brief auto generated.
 * @author K.morningcloud
 * @date 2011/03/11
 */

#include "semi/Controller.h"
#include "semi/TaskManager.h"
#include "semi/NetMessage.h"

using namespace std;
using namespace semi;

int main(int argc, char *argv[]) {

	string opt = "";
	string tsk = "";
	char* msg = (char*)"unknown";
	int siz = 0;
	long slp = 0;
	std::string tmp;

	switch (argc) {

	case 6: // sleep
		tmp = std::string(argv[5]);
		char* err1;
		slp = strtol(tmp.c_str(), &err1, 10);
		if (0 != err1 && 0 != err1[0])
			printf("The forth argument is invalid! %s -> %d \n", tmp.c_str(),
					slp);
		if (60 * 60 < slp) {
			printf("The forth argument is too long! %d -> %d \n", slp, 60 * 60);
			slp = 60 * 60;
		}

	case 5: // size
		tmp = std::string(argv[4]);
		char* err2;
		siz = strtol(tmp.c_str(), &err2, 10);
		if (0 != err2 && 0 != err2[0])
			printf("The forth argument is invalid! %s -> %d \n", tmp.c_str(),
					siz);

	case 4: // message
		msg = argv[3];

	case 3: // taskid
		tsk = string(argv[2]);
		tsk = util::StringUtil::trim(tsk);

	case 2: // disp(0) / init(0) / start(0-1)/ stop(0-1)/ msg(3-4)

		opt = string(argv[1]);
		if ("init" == opt) {
			std::cout << "Initialize " << std::endl;
			//			conf::Configurator::setIninitial();
			//			conf::Configurator::BaseConfig base =
			//					conf::Configurator::getBaseConfig();
			//			std::cout << "base.setup:" << base.setup << std::endl;
			conf::Configurator().setSharedMemory();
			conf::TasksConfig().setSharedMemory();
			conf::NetConfig().setSharedMemory();
		}
		// start
		else if ("start" == opt) {
			if (0 < tsk.length()) {
				cout << "Start " << tsk << endl;
				TaskManager::execute(tsk);
			}
			// Start All
			else {
				cout << "Start All" << endl;
				conf::Configurator::setIninitial();
				TaskManager::executeAll();
			}
			std::cout << CurrentTasks::toString() << std::endl;
		}
		// stop
		else if ("stop" == opt) {

			if (0 < tsk.length()) {
				cout << "Stop " << tsk << endl;

				CurrentTasks(tsk).setValid(false);

				conf::NetConfig::TcpConfig cnf =
						conf::NetConfig().getTcpConfig(tsk);
				if (0 < std::string(cnf.task_id).length() && 0 < cnf.dist_port)
					NetMessage::sweep(cnf);
				else
					ipc::MessageQue(tsk).sweep();
			}
			// Stop All
			else {
				cout << "Stop All" << endl;
				util::Strings ids = CurrentTasks::getCurrentTaskIds();
				for (util::Strings::iterator iter = ids.begin(); iter
						!= ids.end(); iter++) {
					std::string id = (std::string) *iter;
					CurrentTasks(id).setValid(false);
					ipc::MessageQue(id).sweep();
				}
			}
			std::cout << CurrentTasks::toString() << std::endl;
		}
		// log
		else if ("disp" == opt) {
			cout << "Display All" << endl;
			conf::Configurator::BaseConfig base =
					conf::Configurator::getBaseConfig();
			std::cout << "    base.setup:" << base.setup << std::endl;
			std::cout << conf::Configurator::toString() << std::endl;
			std::cout << conf::TasksConfig::toString() << std::endl;
			std::cout << conf::NetConfig::toString() << std::endl;

			std::cout << CurrentTasks::toString() << std::endl;
		}
		// send
		else if ("msg" == opt) {
			if (tsk.length() < 1) {
				cout << "The second argument is empty!" << endl;
				return -1;
			}
			// check current task
			if (!CurrentTasks(tsk).isValid()) {
				cout << "tesk_id of second argument is not alive!" << endl;
				std::cout << CurrentTasks::toString() << std::endl;
				//                return -1;
			}

			semi::util::DateUtil du;
			cout << "Message send->" << tsk << " " << du.getHourString() << ":"
					<< du.getMinString() << " " << du.getSecString()
					<< " delay(sec):" << slp << endl;

			if (0 < slp)
				sleep(slp);

			conf::NetConfig::TcpConfig cnf =
					conf::NetConfig().getTcpConfig(tsk);
			if (0 < std::string(cnf.task_id).length() && 0 < cnf.dist_port)
				NetMessage::send(cnf, msg, siz); // tcp
			else
				ipc::MessageQue(tsk).send(msg, siz); // que

		} else
			cout
					<< "paramater error  semiframe disp/init/start/stop/msg (task id)"
					<< endl;
		break;

	default:
		cout << "paramater error  semiframe disp/init/start/stop/msg (task id)"
				<< endl;
		return -1;
	}

	return 0;
}
